// pages/pickDetails/pickDetails.js
var app = getApp();
var count = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    valueAddedServiceList: [{ id: "09489af7-3dae-48bd-a675-37863876cddb", name: "签单返还", isShow: false }, { id: "b94bd9f0-8b7a-42ae-9dac-49e8d38726be", name: "送新取旧", isShow: false }, { id: "b94bd9f0-8b7a-42ae-9dac-49e8d38726be", name: "代收货款", isShow: false }, { id: "b94bd9f0-8b7a-42ae-9dac-49e8d38726be", name: "返件", isShow: false }],
    modelShowC: true,
    modelShowR: true,
    modelShowZ: true,
    confirm: "请先收款",
    focus: false,
    textareaTitle: "请录入不接单原因",
    inputTitle: "请录入实际收到金额",
    insurance: false,
    itemDetail: "",
    id: "",
    money: 0,
    pickUpPayedMoney: 0,
    pickUpNums: 0,
    totalWeight: 0,
    totalVolume: 0,
    modelShowI: true,
    urlList: [{ name: "提货单", path: "", isOne: true }, { name: "装车前", path: "", isOne: false }, { name: "装车中", path: "", isOne: false }, { name: "装车后", path: "", isOne: false }, { name: "关车门", path: "", isOne: false }, { name: "盖雨棚", path: "", isOne: false }],
    isOther: false,
    AbnormalFeedback:''//异常反馈
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    var that = this
    var itemDetail = app.globalData.driversOrders;
    if (itemDetail.orders.valueAddedServiceList) {
      for (var i = 0; i < itemDetail.orders.valueAddedServiceList.length; i++) {
        for (var j = 0; j < that.data.valueAddedServiceList.length; j++) {
          if (itemDetail.orders.valueAddedServiceList[i].name == that.data.valueAddedServiceList[j].name) {
            that.data.valueAddedServiceList[j].isShow = true
          }
        }
      }
    }
    that.setData({
      valueAddedServiceList: that.data.valueAddedServiceList
    })
    console.log(itemDetail, "itemDetail")
    if (itemDetail.orders.insuredCost > 0) {
      this.setData({
        insurance: true
      })
    }
    app.globalData.id = itemDetail.id
    this.setData({
      itemDetail: app.globalData.driversOrders,
      id: itemDetail.id,
    })
    var pickUpNums = 0,
      totalWeight = 0,
      totalVolume = 0,
      pickUpPayedMoney = itemDetail.orders.totalFee;
    for (let i = 0; i < itemDetail.orders.orderGoodInfosList.length; i++) { //费用体积计算
      pickUpNums += itemDetail.orders.orderGoodInfosList[i].nums;
      // pickUpPayedMoney += itemDetail.orders.orderGoodInfosList[i].freight;
      totalWeight += Number(itemDetail.orders.orderGoodInfosList[i].volumeWeight);
      totalVolume += Number(itemDetail.orders.orderGoodInfosList[i].volume)
    }
    // if()
    var insured = parseFloat(pickUpPayedMoney * itemDetail.orders.insuranceRate).toFixed(2) //保险金额计算
    pickUpPayedMoney = parseFloat(Number(pickUpPayedMoney) + Number(insured)).toFixed(2)
    this.setData({
      pickUpNums: pickUpNums,
      pickUpPayedMoney: pickUpPayedMoney,
      totalWeight: totalWeight,
      totalVolume: totalVolume,
      insured: insured
    })
    console.log(pickUpNums, "pickUpNums", pickUpPayedMoney, "pickUpPayedMoney")

  },
  cargo(e) {
    var item = JSON.stringify(e.currentTarget.dataset.item)
    var idx = e.currentTarget.dataset.index
    app.globalData.orderid = e.currentTarget.dataset.item.orders.orderGoodInfosList[idx].id
    wx.navigateTo({
      url: "../cargozc/cargozc?item=" + item + "&index=" + e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  cancel() {
    this.setData({
      modelShowC: true
    })
  },
  confirm() {
    this.setData({
      modelShowC: true,
      modelShowI: false
    })
  },
  zhuang() {
    this.setData({
      modelShowZ: false
    })
    console.log(this.data.modelShowZ)
  },
  cancelR() {
    this.setData({
      modelShowR: true
    })
  },
  cancelZ() {
    this.setData({
      modelShowZ: true
    })
  },
  cancelI() {
    this.setData({
      modelShowI: true
    })
  },
  confirmI() {
    var that = this,
      item = app.globalData.driversOrders;
    app.getApi.request("/api/DriversOrders/FreightReceive", {
      driversOrders: {
        id: item.id,
        driversOrdersStatus: item.driversOrdersStatus,
        driversOrdersStatusName: item.driversOrdersStatusName,
        pickUpNums: that.data.pickUpNums
      },
      pickUpPayedMoney: that.data.pickUpPayedMoney
    }, "POST", (res) => {
      // , driversOrdersStatusName: e.currentTarget.dataset.statusname, driversOrdersStatus: e.currentTarget.dataset.status, pickUpNums: 0 
      console.log(res)
      if (res.data.responseCode == 1000) {
        app.globalData.driversOrders.orders.receivablesStatus = 2
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000,
          success: function () {
            that.setData({
              modelShowI: true
            })
          }
        });
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      } else {
        wx.showToast({
          title: '收款失败',
          icon: 'loading',
          duration: 2000
        });
      }
    }, (res) => { })

  },
  confirmR() {
    this.setData({
      modelShowR: true
    })
  },
  bindfocus() {
    this.setData({
      focus: true
    })
  },
  bindblur(e) {
    this.setData({
      money: e.detail.value
    })
  },
  reforder() {
    this.setData({
      modelShowR: false
    })
  },
  gathering() { //收款

    this.setData({
      modelShowI: false
    })
  },
  textareaTXT(e) {//异常反馈
    console.log(e.detail.value, "--------")
    var that = this
    that.setData({
      AbnormalFeedback: e.detail.value
    })
  },
  otherFee() { //查看其它费用
    var that = this
    that.setData({
      itemDetail: app.globalData.driversOrders,
      isOther: !that.data.isOther
    })
    // wx.navigateTo({
    //     url: "../other/other"
    // })
  },
  receiving: function () {
    // this.setData({
    //     modelShowC: false
    // })
    var datas = {},
      item = app.globalData.driversOrders;
    console.log(item)
    if (item.orders.payType == 1) { //需要收款
      if (item.orders.receivablesStatus != 1) {
        this.setData({
          modelShowC: false,
        })
        return;
      }
    } else {

    }
    for (let i = 0; i < item.orders.orderGoodInfosList.length; i++) {
      var newObj = {
        DeliveryOrder: {
          id: item.id,
          // driversOrdersStatus: item.driversOrdersStatus,
          // driversOrdersStatusName: item.driversOrdersStatusName,
          // pickUpNums: this.data.pickUpNums
        },
        // orderGoodInfos: {
        //   id: item.orders.orderGoodInfosList[i].id,
        //   long: item.orders.orderGoodInfosList[i].long,
        //   width: item.orders.orderGoodInfosList[i].width,
        //   height: item.orders.orderGoodInfosList[i].height,
        //   volume: item.orders.orderGoodInfosList[i].volume,
        //   realWeight: item.orders.orderGoodInfosList[i].volumeWeight,
        // },
        // orders: item.orders.orderGoodInfosList[i].order,
         AbnormalFeedback: this.data.AbnormalFeedback,//异常反馈
        // realNums: 1,
        outboundList: [{ filePath: this.data.urlList[0].path }], //提货单图片
        carBeforeLoadingPicsList: [{ filePath: this.data.urlList[1].path }], //装车前照片
        carriageLoadsHalfPicsList: [{ filePath: this.data.urlList[2].path }], //厢装货一半照片
        carriageLoadsCompletePicsList: [{ filePath: this.data.urlList[3].path }], //车厢装货完成照片               
        closeDoorPicsList: [{ filePath: this.data.urlList[4].path }], //关联的关闭车门照片
        coverRainforestPicsList: [{ filePath: this.data.urlList[5].path }], //盖雨棚照片
        // pickUpPayedMoney: Number(this.data.pickUpPayedMoney)
      }
       datas=newObj
    }
    var that = this
    app.getApi.request("/api/DeliveryOrder/loading", datas, "POST", (res) => {
      // , driversOrdersStatusName: e.currentTarget.dataset.statusname, driversOrdersStatus: e.currentTarget.dataset.status, pickUpNums: 0 
      console.log(res)
      if (res.data.responseCode == 1000) {

        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000,
          success: function () {

          }
        });
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      } else {
        wx.showToast({
          title: '装车失败',
          icon: 'loading',
          duration: 2000
        });
      }
    }, (res) => { })
  },
  checkboxChange(e) {
    console.log(e.detail.value[0])
    var value = e.detail.value[0]
    this.setData({
      insurance: !this.data.insurance
    })
    if (this.data.insurance) {
      this.setData({
        pickUpPayedMoney: parseFloat(Number(this.data.pickUpPayedMoney) + Number(this.data.insured)).toFixed(2)
      })
    } else {
      this.setData({
        pickUpPayedMoney: parseFloat(Number(this.data.pickUpPayedMoney) - Number(this.data.insured)).toFixed(2)
      })
    }
  },
  deliveryOrder(e) {
    var that = this,
      index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera', 'album'], // 指定来源是相机
      success: function (res) {

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          modelShowZ: true
        })
        that.upLoadImage(tempFilePaths, index)
      }
    })
  },
  empty(e) {
    var index = e.currentTarget.dataset.index,
      imageList = this.data.urlList;

    imageList[index].path = ''
    this.setData({
      urlList: imageList
    })

  },
  receipt() {
    this.setData({
      modelShowZ: false,
      imageType: 4
    })
  },
  camera() {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 指定来源是相机
      success: function (res) {
        count++
        if (count >= 7) {

          return console.log("超出数量");
        }
        console.log("1111", count)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          modelShowZ: true
        })
        that.upLoadImage(tempFilePaths)
      }
    })

  },
  album() {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 指定来源是相册
      success: function (res) {
        count++
        if (count >= 7) {
          return console.log("超出数量");
        }
        console.log("超出数量", count);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          modelShowZ: true
        })
        that.upLoadImage(tempFilePaths)
      }
    })
  },
  upLoadImage(tempFilePaths, index) {
    var that = this,
      imageList = that.data.urlList;
    console.log("上传图片")
    wx.uploadFile({
      url: 'https://driverAPI.dwilms.com/api/Upload/UploadOnePic',
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {},
      success: function (res) {
        console.log(res, "resresres")
        var datas = JSON.parse(res.data)
        if (index || index == 0) {
          imageList[index].path = datas.obj
        } else {
          imageList[count - 1].path = datas.obj
        }
        that.setData({
          urlList: imageList
        })

      }
    })
  },
  UploadMultPic(tempFilePaths) {
    wx.chooseImage({
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://driverAPI.dwilms.com/api/Upload/UploadOnePic', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {},
          success: function (res) {
            var data = JSON.parse(res.data)
          }
        })
      }
    })
  },
  makePhoneCall(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.number
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  totake() {
    var that = this
    var item = JSON.stringify(that.data.itemDetail);
    wx.navigateTo({
      url: "../take/take?item=" + item + "&index=" + 2
    });
  }
})